package com.tkxs.tksbconfig.aspect;

import com.tkxs.tksbconfig.util.AlgorithmUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 配置切点,并且进行配置
 */

@Aspect
@Component
public class ControllerAdvice {

    List<String> listStrings;

    // 用@Pointcut来注解一个切入方法
    @Pointcut("@annotation(com.tkxs.config.demo.anno.MyAnno)")
    public void excudeController() {
    }

    @Around("excudeController()")
    public Object arround(ProceedingJoinPoint pjp) throws Throwable {

        // 获取所有存在的request
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        String url = request.getRequestURL().toString();
        String method = request.getMethod();

        System.out.println("url==>"+url);
        System.out.println("method==>"+method);

        String uniqueKey = AlgorithmUtil.createUniqueKey(url, method);

        doInterceptor(uniqueKey);


        System.out.println("方法环绕start.....");

        try {
            Object o =  pjp.proceed();
            System.out.println("方法环绕proceed，结果是 :" + o);
            return o;
        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
    }

    // 根据uniqueKey 做拦截
    private void doInterceptor(String uniqueKey){



    }
}
